Code:
using System;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
namespace Project1
{
class Class1
{
static void Main()
{
// is called keyhook. does not hook keys, but windows messages
Keyhook.Start();
Application.Run(new Form());
}
}
static public class Keyhook
{
static private IntPtr hKeyboardHook;
static private HookProc KeyboardHookProcedure;
static public void Start()
{
// get the list view
IntPtr lhWndParent = Process.GetProcessesByName("testapp")[0].MainWindowHandle;
IntPtr lhParent = Api.FindWindowEx(lhWndParent, IntPtr.Zero, null, null);
IntPtr lhWndProcessList = Api.GetDlgItem(lhParent, 1);
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
hKeyboardHook = SetWindowsHookEx(4, KeyboardHookProcedure, lhWndParent, 0);
}
static private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
{
MessageBox.Show("works!");
return CallNextHookEx(hKeyboardHook, nCode, (IntPtr)wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
static private extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
}
static public class Api
{
[DllImport("user32.dll", SetLastError = true)]
static public extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
static public extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
static public extern bool EnableWindow(IntPtr hWnd, bool bEnable);
[DllImport("user32.dll")]
static public extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static public extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);
[DllImport("user32.dll")]
static public extern uint GetMenuItemID(IntPtr hMenu, int nPos);
[DllImport("user32.dll")]
static public extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
[DllImport("user32.dll")]
static public extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr SendMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int msg, IntPtr wParam, ref TvItem item);
[DllImport("user32.dll")]
static public extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, IntPtr lParam);
[DllImport("user32.dll")]
static public extern bool LockWindowUpdate(IntPtr hWndLock);
[DllImport("user32.dll")]
static public extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static public extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
[DllImport("kernel32.dll")]
static public extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
static public extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static public extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static public extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
static public extern bool ReadProcessMemory(IntPtr hProcess, IntPtr baseAddress, byte[] buffer, int dwSize, out int numberOfBytesRead);
[DllImport("kernel32.dll")]
static public extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, int lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
static public extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref TvItem buffer, int dwSize, IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
static public extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static public extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref LvItem buffer, int dwSize, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static public extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, IntPtr lpNumberOfBytesRead);
[DllImport("user32.dll", SetLastError = true)]
static public extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
static public extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out int lpwdProcessID);
[StructLayout(LayoutKind.Sequential)]
public struct LvItem
{
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
}
[StructLayout(LayoutKind.Sequential)]
public struct TvItem
{
public int mask;
public IntPtr hItem;
public int state;
public int stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
public int iIntegral;
}
public struct Rect
{
int left, top, right, bottom;
}
public struct Point
{
int x, y;
}
public struct WindowPlacement
{
public int length, flags, showCmd;
public Point ptMinPosition, ptMaxPosition;
public Rect rcNormalPosition;
}
}
}